package com.avcdata.national.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.activity.Login_Activity;
import com.avcdata.aoscustommade.activity.Main_Activity;
import com.avcdata.aoscustommade.fragment.SlideMenuFragment;
import com.avcdata.aoscustommade.main.BaseActivity;
import com.avcdata.aoscustommade.utils.AppActivityManager;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.avcdata.national.module.UserCompetingBrandInfo;
import com.avcdata.national.network.JsonRunnable;
import com.avcdata.national.network.ThreadPoolUtils;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.mxn.soul.flowingdrawer_core.FlowingView;
import com.mxn.soul.flowingdrawer_core.LeftDrawerLayout;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * NewAOSApp
 * com.avcdata.national.activity
 * Created by lic on 2017/8/17.
 * CSDN http://blog.csdn.net/lucasey
 */

public class ModuleSelectActivity extends BaseActivity {
    //侧滑相关
    @ViewInject(R.id.id_drawerlayout)
    private LeftDrawerLayout mLeftDrawerLayout;
    @ViewInject(R.id.sv)
    private FlowingView mFlowingView;

    @ViewInject(R.id.national)
    private RelativeLayout national;
    @ViewInject(R.id.account)
    private LinearLayout account;
    @ViewInject(R.id.area_manager)
    private RelativeLayout area_manager;

    @ViewInject(R.id.jiazai_layout)
    private LinearLayout loading;

    @Override
    public void initView() {
        setContentView(R.layout.activity_select_module);
        AppActivityManager.getAppManager().addActivity(this);
        ViewUtils.inject(this);
    }

    @Override
    public void initInstall() {
        loading.setVisibility(View.GONE);
        national.setOnClickListener(this);
        area_manager.setOnClickListener(this);
        account.setOnClickListener(this);
        FragmentManager fm = getSupportFragmentManager();
        SlideMenuFragment mMenuFragment = (SlideMenuFragment) fm.findFragmentById(R.id.id_container_menu);
        if (mMenuFragment == null) {
            fm.beginTransaction().add(R.id.id_container_menu, mMenuFragment = new SlideMenuFragment()).commit();
        }
        mLeftDrawerLayout.setFluidView(mFlowingView);
        mLeftDrawerLayout.setMenuFragment(mMenuFragment);
    }

    @Override
    public void initData() {

    }

    @Override
    public void evenControl() {

    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()){
            case R.id.national:
                if (isAbleClick()) {
                    getCompeting();
                }else {
                    mLeftDrawerLayout.toggle();
                }
                break;
            case R.id.area_manager:
                if (isAbleClick()) {
                    Intent intent = new Intent(ModuleSelectActivity.this, Main_Activity.class);
                    startActivity(intent);
                }else {
                    mLeftDrawerLayout.toggle();
                }
                break;
            case R.id.account:
                //展开侧滑页
                if (!mLeftDrawerLayout.isShownMenu()) {
                    mLeftDrawerLayout.toggle();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 是否可以点击
     *
     * @return
     */
    private boolean isAbleClick() {
        if (mLeftDrawerLayout.isShownMenu()) {
            return false;
        }
        return true;
    }

    /***
     * 获取页面信息
     */
    private void getCompeting(){
        loading.setVisibility(View.VISIBLE);
        HashMap<String,Object> map =new HashMap<>();
        map.put("lastWeek", new SharedPreferencesUtils(this).getNewWeek());
        map.put("selfbrand","A.O.史密斯");
        ThreadPoolUtils.execute(new JsonRunnable(ModuleSelectActivity.this,"HTTPGET", AvcConstants.URLHeaderBase+AvcConstants.URL_CompetingBrand,handler,map,2));
    }

    private long startTime = 0;//开始点击的时间,全局变量
    @Override
    public void onBackPressed() {
        if (mLeftDrawerLayout.isShownMenu()) {
            mLeftDrawerLayout.closeDrawer();
        } else {
            if (System.currentTimeMillis() - startTime > AvcConstants.EXITTIME) {
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.exit_again), Toast.LENGTH_SHORT).show();
                startTime = System.currentTimeMillis();
            } else {
                AppActivityManager.getAppManager().finishAllActivity();
            }

        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case -1:
                    loading.setVisibility(View.GONE);
                    break;
                case 2:
                    loading.setVisibility(View.GONE);
                    boolean isSelected = false;
                    UserCompetingBrandInfo info  = new Gson().fromJson(msg.obj.toString(),UserCompetingBrandInfo.class);
                    StringBuffer sb = new StringBuffer();
                    if(info.getReturnValue().get(0).getCompetingBrands().size() == 0 &&
                            info.getReturnValue().get(1).getCompetingBrands().size() == 0 &&
                            info.getReturnValue().get(2).getCompetingBrands().size() == 0 &&
                            info.getReturnValue().get(3).getCompetingBrands().size() == 0 ){
                        Intent intent1 = new Intent(ModuleSelectActivity.this, PersonalAttentionCategoryActivity.class);
                        intent1.putExtra("where","main");
                        startActivity(intent1);
                    }else{
                        for(int i=0;i<info.getReturnValue().size();i++){
                            Set<String> brands = new HashSet();
                            for(int j=0;j<info.getReturnValue().get(i).getCompetingBrands().size();j++){
                                brands.add(info.getReturnValue().get(i).getCompetingBrands().get(j).trim());
                            }
                            new SharedPreferencesUtils(ModuleSelectActivity.this).setCompetingBrands(info.getReturnValue().get(i).getCategory(),brands);
                            if(info.getReturnValue().get(i).getCompetingBrands().size() !=0){
                                sb.append(info.getReturnValue().get(i).getCategory()+",");
                            }
                        }
                        new SharedPreferencesUtils(ModuleSelectActivity.this).setCompetingCategory(sb.deleteCharAt(sb.length()-1).toString());
                        Intent intent1 = new Intent(ModuleSelectActivity.this, NationalOverViewActivity.class);
                        startActivity(intent1);
                    }
                    break;
                case -22:
                    loading.setVisibility(View.GONE);
                    Intent intent = new Intent ();
                    intent.setClass (ModuleSelectActivity.this, Login_Activity.class );
                    intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
                    ModuleSelectActivity.this.finish ();
                    startActivity ( intent );
                    break;
                default:
                    loading.setVisibility(View.GONE);
                    break;
            }

        }
    };

}